//
//  CenterViewController.m
//  QuickYi
//
//  Created by 张贞献 on 16/5/9.
//  Copyright © 2016年 张贞献. All rights reserved.
//

#import "CenterViewController.h"

@interface CenterViewController ()<UIScrollViewDelegate>
@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) UIPageControl *pageControl;

@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic, strong)UIView * weatherView;
@property(nonatomic, strong)UIImageView * iconImageView;
@end

@implementation CenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpNav];
    [self setUpHomeUI];
}

#pragma mark 设置导航栏
-(void)setUpNav {
    
    self.navigationItem.title = @"马医一生";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav"] forBarMetrics:UIBarMetricsDefault];
    //navigationBar 的titlte颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //添加右侧按钮
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"illness_button_input_blue.png"] style:UIBarButtonItemStylePlain target:self action:@selector(jump2OtherController)];
    
    [self.navigationItem setRightBarButtonItem:rightItem animated:YES];
    
}
#pragma mark  TOGO:
-(void)setUpHomeUI {
    //天气模块
    [self setWeatherUI];
    //轮播器
    [self setLunImage];
    
    
}
#pragma mark 天气模块
-(void) setWeatherUI {
    UIView * weatherView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 80)];
    weatherView.backgroundColor = [[UIColor alloc]initWithRed:196.0/256.0 green:240.0/256.0 blue:226.0/256.0 alpha:1.0];
    self.weatherView = weatherView;

    [self.view addSubview:weatherView];
    
    //设置中间的view
    
    UIView * minddleView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.size.width * 0.5 , 5, 1, 70)];
    minddleView.backgroundColor = [UIColor grayColor];
    [weatherView addSubview: minddleView];
    
    //天气图像
    UIImageView * weatherImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"yin"]];
    [weatherView addSubview: weatherImage];
    // 防止block中的循环引用
    __weak typeof (self) weakSelf = self;
    [weatherImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(weakSelf.weatherView).offset(20);
        make.size.mas_equalTo(CGSizeMake(46, 30));
    }];
    
    UILabel * weatherLabel = [[UILabel alloc] init];
    weatherLabel.text = @"小雨转多云";//应该是模型里的类型
    //换行
    weatherLabel.numberOfLines = 0;
    weatherLabel.font = [UIFont systemFontOfSize:14];
    weatherLabel.textColor =[ [UIColor alloc]initWithRed:96.0/256.0 green:240.0/256.0 blue:126.0/256.0 alpha:1.0];
    
    [weatherView addSubview: weatherLabel];
    
    [weatherLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.equalTo(weakSelf.weatherView).offset(15);
        make.left.equalTo(weatherImage.mas_right).offset(20);
        make.size.mas_equalTo(CGSizeMake(50, 50));

    }];
    
    UILabel * weatherCity = [[UILabel alloc] init];
    weatherCity.text = @"上海";//应该是模型里的类型
   
    weatherCity.font = [UIFont systemFontOfSize:17];
    weatherCity.textColor =[ [UIColor alloc]initWithRed:96.0/256.0 green:40.0/256.0 blue:26.0/256.0 alpha:1.0];
    
    [weatherView addSubview: weatherCity];
    
    [weatherCity mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.weatherView).offset(15);
        make.left.equalTo(weatherLabel.mas_right).offset(10);
        make.size.mas_equalTo(CGSizeMake(50, 50));
        
    }];
    
    //右侧日期 和温度
    UILabel * temperature = [[UILabel alloc] init];
    temperature.text = @"温度：19~21 ";//应该是模型里的类型
    
    temperature.font = [UIFont systemFontOfSize:14];
    temperature.textColor =[ [UIColor alloc]initWithRed:96.0/256.0 green:40.0/256.0 blue:26.0/256.0 alpha:1.0];
    
    [weatherView addSubview: temperature];
    
    [temperature mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(weakSelf.weatherView).offset(20);
        make.left.equalTo(minddleView).offset(25);
        make.height.offset(10);
        make.right.equalTo(weakSelf.weatherView).offset(-10);
        
    }];

    UILabel * weatherData = [[UILabel alloc] init];
    weatherData.text = @"日期： 05月11日 ";//应该是模型里的类型
    
    weatherData.font = [UIFont systemFontOfSize:14];
    weatherData.textColor =[ [UIColor alloc]initWithRed:96.0/256.0 green:40.0/256.0 blue:26.0/256.0 alpha:1.0];
    
    [weatherView addSubview: weatherData];
    
    [weatherData mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(temperature.mas_bottom).offset(20);
        make.left.equalTo(minddleView).offset(25);
        make.height.offset(10);
        make.right.equalTo(weakSelf.weatherView).offset(-10);
        
    }];
    
}
//轮播器
-(void)setLunImage{
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    
    [self.view addSubview:scrollView];
    
    scrollView.backgroundColor = [UIColor redColor];
    
    self.scrollView = scrollView;
    // 防止block中的循环引用
        __weak typeof (self) weakSelf = self;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(self.view.bounds.size.width , 200));
        
        make.top.equalTo(weakSelf.weatherView.mas_bottom).offset(0);
    
    }];
    
//    UIImageView * iconImageView = [[UIImageView alloc] init];
//    iconImageView.backgroundColor = [UIColor grayColor];
//    self.iconImageView = iconImageView;
//    
//    [self.scrollView addSubview: iconImageView];
//    
//    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.size.mas_equalTo(scrollView);
//        make.top.equalTo(weakSelf.weatherView.mas_bottom).offset(0);
//        
//        
//    }];
    
    
    
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    
    self.pageControl = pageControl;
    
    [self.view addSubview: pageControl];
    
    [pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.offset(39);
        make.top.equalTo(weakSelf.weatherView.mas_bottom).offset(150);
        make.centerX.equalTo(weakSelf.view);
        
        
    }];
    
    
    NSArray *imagesArray = @[@"http://7xsiiq.com2.z0.glb.clouddn.com/h1.png",
                             @"http://7xsiiq.com2.z0.glb.clouddn.com/h2.png",
                             @"http://7xsiiq.com2.z0.glb.clouddn.com/h3.png"];
    
    //NSMutableArray *imageViewArray = [NSMutableArray array];
    
    for (int i = 0; i < imagesArray.count; i++) {
        
        UIImageView *imageView =[[UIImageView alloc]init];
        imageView.frame = CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height);

//        imageView = self.iconImageView;
        
        [self.scrollView addSubview:imageView];
//        NSString * img = [NSString stringWithFormat:@"http://7xsiiq.com2.z0.glb.clouddn.com/h%d.png",i + 1];
//
//        [imageView sd_setImageWithURL:[NSURL URLWithString:img] placeholderImage:[UIImage imageNamed:@"content_img_03"]];
        //设置图片
        [imageView sd_setImageWithURL:[NSURL URLWithString:imagesArray[i]] placeholderImage:[UIImage imageNamed:@"content_img_03"] options:SDWebImageRetryFailed];

//        [imageViewArray addObject:imageView];
     
    }
    
    //    2.设置滚动范围
    self.scrollView.contentSize = CGSizeMake( imagesArray.count * self.scrollView.frame.size.width, 0);
    //    3.设置分页
    self.scrollView.pagingEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    //    4.设置总的页数
    self.pageControl.numberOfPages = imagesArray.count;
    self.pageControl.tintColor = [UIColor blueColor];
    self.pageControl.currentPageIndicatorTintColor = [UIColor greenColor];

    //    5.设置当前页
    self.pageControl.currentPage = 0;
    //    6.设置代理
    self.scrollView.delegate = self;
    //    scheduledTimerWithTimeInterval定时器执行的时间
    
    //    表示开启定时器后随来监听执行的操作，一般是控制器（self）
    
    //   创建一个 定时器，让每隔一段时间执行某个操作(自动开启)
    [self addTimer];
    //    7，修改定时器添加的模式，进行分流，同时处理定时器和拖拽
    [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSDefaultRunLoopMode];
}
//实现下一张图片的功能
- (void)next{
    
    //    1.获取当前页码
    NSInteger page = self.pageControl.currentPage;
    
    //
    if (page== 2) {
        page = 0;
        
    }else{
       
        page++;
    }
    
    //    2.根据页码设置偏移量，让控件动起来
    [self.scrollView setContentOffset:CGPointMake(page *self.scrollView.frame.size.width, 0) animated:YES];
    
    
}


//整个滚动过程
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //    1.计算当前页的页码
    
    //      1.1获取偏移量
    CGFloat offsetX =   self.scrollView.contentOffset.x  + 0.5 *self.scrollView.frame.size.width;
    NSInteger  page =  offsetX /self.scrollView.frame.size.width;
    
    //    2.设置当前的页码
    self.pageControl.currentPage = page;
    
     
}

//监听scrollView的开始拖拽，当开始拖拽的时候停止定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    //    1.停止定时器.定时器一旦停止不能再开启，如果想要继续使用定时器，需要重写创建
    [self.timer invalidate];
    self.timer = nil;
    
}
//当停止拖拽的时候，重新创建定时器
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    [self addTimer];
    
}
//添加定时器
-(void)addTimer{
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(next) userInfo:nil repeats:YES];
    self.timer  = timer;
    
    
}

#pragma mark  TOGO: 右侧跳转按钮
-(void)jump2OtherController {
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
